<template>
  <div class="echars-bar">
    <div :ref="`echarts-bar`" class="two-bar-line" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
export default {
  name: 'EchartsOneBarLine',
  props: {
    dataList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      echarts: null,
      // 柱子类型及数据
      gradeList: [],
      projectTotal: [], // 项目总数
      contract: [] // 合同总金额
    }
  },
  computed: {
    ...mapState({
      proGrade: state => state.basicInfo.proGrade // 项目等级
    })
  },
  watch: {
    dataList(values) {
      if (values && values.length) {
        this.initEcharts(values)
      }
    }
  },
  mounted() {
    this.initEcharts(this.dataList)
  },
  beforeDestroy() { // 清理工作 避免内存泄漏
    // 销毁监听事件
    window.removeEventListener('resize', this.resizeHandler)
    // 销毁 Echarts 实例
    this.echarts && this.echarts.dispose()
  },
  methods: {
    initEcharts(values) {
      this.projectTotal = []
      this.contract = []
      this.gradeList = []
      values.forEach(item => {
        this.projectTotal.push(item.countCountyPro)
        this.contract.push((item.totalMoney / 10000).toFixed(6))
        if (item.ptGrade) this.gradeList.push(this.proGrade.filter(i => i.dictValue === item.ptGrade)[0].dictLabel)
      })
      console.log(this.contract)
      const options = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 10,
          icon: 'circle',
          data: ['项目总数', '合同总金额'],
          textStyle: {
            color: '#6AA4F5'
          }
        },
        grid: {
          right: '20%',
          bottom: 25
        },
        xAxis: {
          type: 'category',
          data: this.gradeList,
          axisLabel: {
            color: '#fff',
            fontSize: 12
          // interval: 0
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#12fffe',
              width: 1,
              type: 'solid'
            }
          }
        },
        yAxis: [
          {
            name: '(个)',
            type: 'value',
            min: 0,
            max: 5,
            minInterval: 1,
            nameTextStyle: {
              color: '#fff'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#eee',
              fontSize: 12
            },
            splitLine: {
              lineStyle: {
                color: ['#12fffe'],
                type: 'dotted'
              }
            }
          },
          {
            name: '(万元)',
            type: 'value',
            nameTextStyle: {
              color: '#fff'
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#eee',
              fontSize: 12
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            }

          }
        ],
        series: [
          {
            name: '项目总数',
            data: this.projectTotal,
            type: 'bar',
            barWidth: '6px',
            itemStyle: {
              color: '#00ffe6',
              barBorderRadius: [180]
            },
            yAxisIndex: 0
          },
          {
            name: '合同总金额',
            data: this.contract,
            type: 'line',
            symbolSize: 6,
            symbol: 'circle',
            itemStyle: {
              color: 'rgb(255,237,8)', // 折线点的颜色
              lineStyle: {
                color: 'rgb(255,237,8)' // 折线的颜色
              }
            },
            yAxisIndex: 1
          }
        ]
      }
      this.echarts = echarts.init(this.$refs[`echarts-bar`])
      this.echarts.setOption(options)
      // 绑定监听事件
      window.addEventListener('resize', this.resizeHandler)
    },
    resizeHandler() {
      this.echarts.resize()
    }
  }
}
</script>

<style lang="scss" scoped>
  .echars-bar{
    .two-bar-line{
      height:260px;
    }
  }
</style>
